package mshop.com.schild_kotlin.skin

import android.app.Application
import android.os.Build
import android.support.v4.graphics.drawable.DrawableCompat
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.widget.ImageView
import android.widget.ListPopupWindow
import android.widget.RadioButton
import android.widget.TextView
import com.wind.me.xskinloader.SkinInflaterFactory
import com.wind.me.xskinloader.SkinManager
import mshop.com.schild_kotlin.BuildConfig.isOpenSkin
import mshop.com.schild_kotlin.application.SchildApplicationLike

/**
 * Created by mayi on 2018/6/1.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO
 */
class MySkinUtil {

    companion object {
        fun onCreateInit(activity: AppCompatActivity) {
            if (isOpenSkin) SkinInflaterFactory.setFactory(activity)
        }

        fun onApplicationInit(application: Application) {
            if (isOpenSkin) {
                //如果代码中需要经常使用Application Context的LayoutInflater加载View，最好同时加上这样一行代码
                SkinInflaterFactory.setFactory(LayoutInflater.from(application))
                SkinManager.get().init(application)
            }
        }

        //动态设置资源
        fun setWindowStatusBarColor(window: Window, colorResId: Int) {
            if (isOpenSkin) {
                SkinManager.get().setWindowStatusBarColor(window, colorResId)
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    window.statusBarColor = SchildApplicationLike.schildApplication?.resources!!.getColor(colorResId)
                }
            }

        }

        fun setImageDrawable(imageView: ImageView, imgResId: Int) {
            if (isOpenSkin) {
                SkinManager.get().setImageDrawable(imageView, imgResId);
            } else {
                imageView.setImageResource(imgResId)
            }
        }

        fun setImageTint(imageView: ImageView, colorResId: Int) {
            if (isOpenSkin) {
                SkinManager.get().setImageTint(imageView, colorResId);
            } else {
                val wrappedDrawable = DrawableCompat.wrap(imageView.drawable)
                DrawableCompat.setTint(wrappedDrawable, imageView.resources.getColor(colorResId))
            }
        }

        fun setViewBackground(view: View, backResId: Int) {
            if (isOpenSkin) {
                SkinManager.get().setViewBackground(view, backResId)
            } else {
                view.setBackgroundResource(backResId)
            }
        }

        fun setTextViewColor(view: View, colorResId: Int) {
            if (isOpenSkin) {
                SkinManager.get().setTextViewColor(view, colorResId)
            } else if (view is TextView) {
                view.setTextColor(colorResId)
            }
        }

    }


}